//
//  MyActionSheetView.m
//  Qianbancaifu
//
//  Created by SYLH on 16/5/19.
//  Copyright © 2016年 SYLH. All rights reserved.
//

#import "MyActionSheetView.h"


@interface MyActionSheetView ()

@property (nonatomic,strong) UIView * actionView;

@property (nonatomic,strong) UIView * contentView;

@property (nonatomic,assign) CGFloat contentHeight;

@property (nonatomic,strong) UIButton * cancelBtn;

@end

@implementation MyActionSheetView



-(instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray  withColorArray:(NSArray *)colorArray actionBlock:(ClickActionIndexBlock)actionBlock cancalBlock:(ActionCancalBlock)cancalBlock
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        
        self.clickActionIndexBlock = actionBlock;
        self.actionCancalBlock = cancalBlock;
        
        self.contentHeight = 4 + 15 + titleArray.count * 46 + 45;
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, KScreen_Height, KScreen_Width,  self.contentHeight)];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.contentView];
        
        self.actionView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, KScreen_Width - 10,  titleArray.count * 46 - 1)];
        self.actionView.backgroundColor = [UIColor clearColor];
        self.actionView.layer.cornerRadius = 10;
        self.actionView.clipsToBounds = YES;
        [self.contentView addSubview:self.actionView];
        
        for (int i = 0; i < titleArray.count; i++) {
            UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 46 * i, KScreen_Width - 10, 45)];
            [btn setTitle:titleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:colorArray[i] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            btn.titleLabel.font = PFR_FONT(18);
            btn.tag = 10 + i;
            btn.exclusiveTouch = YES;
            [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.actionView addSubview:btn];
        }
        
        self.cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(5,CGRectGetMaxY(self.actionView.frame) + 15, KScreen_Width - 10, 45)];
        [self.cancelBtn setTitle:KNSLocal(@"alertCancel") forState:UIControlStateNormal];
        [self.cancelBtn setTitleColor:[UIColor colorWithHex:0x525151 alpha:1.0] forState:UIControlStateNormal];
        self.cancelBtn.backgroundColor = [UIColor whiteColor];
        self.cancelBtn.titleLabel.font = PFR_FONT(18);
        self.cancelBtn.layer.cornerRadius = 10;
        self.cancelBtn.clipsToBounds = YES;
        self.cancelBtn.tag = -1;
        [self.cancelBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.cancelBtn];
        
        

        
        [UIView animateWithDuration:0.3 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.contentView.transform = CGAffineTransformMakeTranslation(0, -self.contentHeight);
        } completion:^(BOOL finished) {
        }];
    }
    return self;
}

#pragma mark - 点击action
-(void)clickAction:(UIButton *)sender
{
    NSInteger index = sender.tag - 10;
    if (index >= 0) {
        self.clickActionIndexBlock(index);
        [UIView animateWithDuration:0.2  animations:^{
            self.contentView.transform = CGAffineTransformMakeTranslation(0, 0);
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }else{
        self.actionCancalBlock();
        [UIView animateWithDuration:0.2 animations:^{
            self.contentView.transform = CGAffineTransformMakeTranslation(0, 0);
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeView];
}

-(void)setCancelColor:(UIColor *)cancelColor
{
    _cancelColor = cancelColor;
    [self.cancelBtn setTitleColor:_cancelColor forState:UIControlStateNormal];
}

-(void)removeView
{
    self.actionCancalBlock();
    [UIView animateWithDuration:0.2  animations:^{
        self.contentView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
